package org.rcloud.io.service;

/**
 * Created by Administrator on 2019/3/6.
 */

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.rcloud.io.service.msnmodel.AccessToken;
import org.rcloud.io.service.msnmodel.ConstantUtils;
import org.rcloud.io.service.msnmodel.TemplateData;
import org.rcloud.io.service.msnmodel.WxMssVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by qcl on 2019/3/6
 * 微信小程序推送服务，
 * 包含获取access_token的服务
 */
@Service
@Slf4j
public class WxPushServiceQcl {
    //用来请求微信的get和post

    @Autowired
    private RestTemplate restTemplate;

    private Map<String, TemplateData> data;//推送文字

    /*
    * 微信小程序推送单个用户
    * formid:是前端给的
    * */
    public String pushOneUser(String openid, String formid, String username,
                              Date time, String info2,String adress,String userphone) {
        System.out.println("=======结亲报名---消息模板=======:");
        //时间格式转换
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = formatter.format(time);
        //获取access_token
        String access_token = getAccess_token(ConstantUtils.SCHOOL_APPID, ConstantUtils.SCHOOL_APPSECRET);
        System.out.println("====access_token=====:"+access_token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send" +
                "?access_token=" + access_token;

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        wxMssVo.setTemplate_id(ConstantUtils.SCHOOL_TEMPLATE_ID);//结亲模版id
        wxMssVo.setForm_id(formid);//formid
        //定义消息模板显示的内容
        Map<String,TemplateData> map = new HashMap<>();
        map.put("keyword1",new TemplateData(username));
        map.put("keyword2",new TemplateData(times));
        map.put("keyword3",new TemplateData(info2));
        map.put("keyword4",new TemplateData(adress));
        map.put("keyword5",new TemplateData(userphone));
        wxMssVo.setData(map);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        //log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();

    }

    //因为传的参数个数不同，所以复制了一份
    public String pushOneUser2(String openid, String formid, String linename,
                               String name,String telephone,String peoplenum,Date time) {
        System.out.println("=======旅游报名---消息模板=======:");
        System.out.println("-----openid------:"+openid+"---formid----:"+formid+"---linename----:"+linename+
                "----name---:"+name+"----time---:"+time);
        //时间格式转换
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = formatter.format(time);
        //获取access_token
        String access_token = getAccess_token(ConstantUtils.SCHOOL_APPID, ConstantUtils.SCHOOL_APPSECRET);
        System.out.println("====access_token=====:"+access_token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send" +
                "?access_token=" + access_token;

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        wxMssVo.setTemplate_id(ConstantUtils.SCHOOL_TEMPLATE_ID2);//旅游模版id
        wxMssVo.setForm_id(formid);//formid

        Map<String,TemplateData> map = new HashMap<>();
        map.put("keyword1",new TemplateData(linename));
        map.put("keyword2",new TemplateData(name));
        map.put("keyword3",new TemplateData(telephone));
        map.put("keyword4",new TemplateData(peoplenum));
        map.put("keyword5",new TemplateData(times));
        wxMssVo.setData(map);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        //log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();

    }

    //因为传的参数个数不同，所以复制了一份
    public String pushOneUser3(String openid, String formid, String activityname,
                               String username,String telephone,Date time) {
        System.out.println("=======摄影大赛报名---消息模板=======:");
        System.out.println("-----openid------:"+openid+"---formid----:"+formid+"---activityname----:"+activityname+
                "----username---:"+username+"----time---:"+time);
        //时间格式转换
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = formatter.format(time);
        //获取access_token
        String access_token = getAccess_token(ConstantUtils.SCHOOL_APPID, ConstantUtils.SCHOOL_APPSECRET);
        System.out.println("====access_token=====:"+access_token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send" +
                "?access_token=" + access_token;

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        wxMssVo.setTemplate_id(ConstantUtils.SCHOOL_TEMPLATE_ID3);//摄影模版id
        wxMssVo.setForm_id(formid);//formid

        Map<String,TemplateData> map = new HashMap<>();
        map.put("keyword1",new TemplateData(activityname));
        map.put("keyword2",new TemplateData(username));
        map.put("keyword3",new TemplateData(telephone));
        map.put("keyword4",new TemplateData(times));
        wxMssVo.setData(map);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        //log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();

    }

    public String pushOneUser4(String openid, String formid, String activityname,
                               String username,String sex,String age,String telephone,Date time) {
        System.out.println("=======寻美评美报名---消息模板=======:");
        System.out.println("-----openid------:"+openid+"---formid----:"+formid+"---activityname----:"+activityname+
                "----username---:"+username+"----sex---:"+sex+"----age---:"+age+"----time---:"+time);
        //时间格式转换
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = formatter.format(time);
        //获取access_token
        String access_token = getAccess_token(ConstantUtils.SCHOOL_APPID, ConstantUtils.SCHOOL_APPSECRET);
        System.out.println("====access_token=====:"+access_token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send" +
                "?access_token=" + access_token;

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        wxMssVo.setTemplate_id(ConstantUtils.SCHOOL_TEMPLATE_ID4);//摄影模版id
        wxMssVo.setForm_id(formid);//formid

        Map<String,TemplateData> map = new HashMap<>();
        map.put("keyword1",new TemplateData(activityname));
        map.put("keyword2",new TemplateData(username));
        map.put("keyword3",new TemplateData(sex));
        map.put("keyword4",new TemplateData(age));
        map.put("keyword5",new TemplateData(telephone));
        map.put("keyword6",new TemplateData(times));
        wxMssVo.setData(map);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        //log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();
    }

    public String pushOneUser5(String openid, String formid, String activityname,
                               String username,String telephone,String remark,Date time) {
        System.out.println("=======摄影大赛报名---消息模板=======:");
        System.out.println("-----openid------:"+openid+"---formid----:"+formid+"---activityname----:"+activityname+
                "----username---:"+username+"---telephone----:"+telephone
                +"---remark----:"+remark+"----time---:"+time);
        //时间格式转换
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = formatter.format(time);
        //获取access_token
        String access_token = getAccess_token(ConstantUtils.SCHOOL_APPID, ConstantUtils.SCHOOL_APPSECRET);
        System.out.println("====access_token=====:"+access_token);
        String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send" +
                "?access_token=" + access_token;

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        wxMssVo.setTemplate_id(ConstantUtils.SCHOOL_TEMPLATE_ID5);//摄影模版id
        wxMssVo.setForm_id(formid);//formid

        Map<String,TemplateData> map = new HashMap<>();
        map.put("keyword1",new TemplateData(activityname));
        map.put("keyword2",new TemplateData(username));
        map.put("keyword3",new TemplateData(telephone));
        map.put("keyword4",new TemplateData(remark));
        map.put("keyword5",new TemplateData(times));
        wxMssVo.setData(map);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        //log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();

    }



    /*
    * 获取access_token
    * appid和appsecret到小程序后台获取，当然也可以让小程序开发人员给你传过来
    * */
    public String getAccess_token(String appid, String appsecret) {
        //获取access_token
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
                "&appid=" + ConstantUtils.SCHOOL_APPID + "&secret=" + ConstantUtils.SCHOOL_APPSECRET;
//        String json = restTemplate.getForObject(url, String.class);
        String json = "";
        try{
            RestTemplate restTemplate = new RestTemplate();
            json = restTemplate.getForObject(url, String.class);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        System.out.println("====json=====:"+json);
        AccessToken accessToken = JSONObject.parseObject(json, AccessToken.class);
        System.out.println("====accessToken=====:"+accessToken);
        return accessToken.getAccess_token();
    }

}

